<?php

namespace app\api\controller\user;

use app\admin\model\User;
use app\admin\model\Worktype;
use app\common\controller\Api;
use app\admin\model\Information as InformationModel;
use think\Cache;
use think\Request;

class Information extends Api
{
    protected $noNeedLogin = ['sendCode','verifyCode'];
    protected $noNeedRight = '*';

    //发送验证码
    public function sendCode()
    {
        // 获取手机号码
        $phone = $this->request->post('mobile');
        if (!$phone){
            return $this->error('请输入手机号');
        }
        // 生成验证码，这里简单地生成一个 6 位随机数字
        $verificationCode = rand(100000, 999999);
        $verificationCode = '123456';
        // 存储验证码到缓存，过期时间设置为 2 分钟（120 秒）
        $cacheKey = 'verification_code_' . $phone;
        $cache = Cache::set($cacheKey, $verificationCode, 300);
        if($cache){
            return $this->success('发送成功',$cacheKey);
        }

    }
    //验证验证码
    public function verifyCode()
    {
        // 获取手机号码和用户输入的验证码
        $phone = $this->request->post('mobile');
        $userCode = $this->request->post('code');
        $cacheKey = $this->request->post('phoneKey');;

        // 获取缓存中存储的验证码
        $storedCode = Cache::get($cacheKey);
        if ($storedCode && $userCode == $storedCode) {
            // 验证码验证成功，清除缓存
            Cache::rm($cacheKey);
            return true;
        } else {
            // 验证码验证失败
            return $this->error('验证码错误');
        }
    }
    //获取个人信息
    public function getInformation()
    {
        $userId = $this->auth->id;
        $user = User::alias('user')->where('user.id',$userId)->with('type')->find();
        $director = User::where('id', $user->type->director)->find();
        $user->type['directors'] = $director;
        $manager = User::where('id', $user->type->manager)->find();
        $user->type['managers'] = $manager;
//        $user->type->directors;
//        $user->type->managers;
        $this->success('获取成功',$user);
    }
    //提交审核
    public function uploadInformation()
    {

        $userId = $this->auth->id;
        $requestData = $this->request->post();
        $requestData['user_id'] = $userId;
        $code = $this->verifyCode();
        if($code){
            $information = new InformationModel();
            unset($requestData['id']);
            unset($requestData['state']);
            $new = $information->allowField(true)->save($requestData);
            User::where('id',$userId)->update([
               'state'=>1
            ]);
            if($new){
                $this->success('提交成功,等待审核',$new);
            }
        }

    }
    //获取工种列表
    public function getWokeType(Request $request)
    {
        $search = $request->get('search');
        if($search){
            $user = Worktype::where('name','like','%'.$search.'%')->select();
        }else {
            $user = Worktype::with(['directors','managers'])->select();
        }
        $this->success('获取成功',$user);
    }
    //修改头像
    public function updateAvatar()
    {
        $avatar = $this->request->post('avatar');
        $userId = $this->auth->id;
        User::where('id',$userId)->update([
           'avatar' =>$avatar
        ]);
        $this->success('修改成功');
    }
}